<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Country_setting_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $country_id = 0;
	var $country_code ="";
	var $payment_gateway_ids = '';
	var $tax = 0;
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setId($input){
		$this->id=$input;
	}
	function setCountryId($input){
		$this->country_id=$input;
	}
	function setCountryCode($input){
		$this->country_code=$input;
	}
	function setPaymentGatewayIds($input){
		$this->payment_gateway_ids = $input;
	}
	function setTax($input){
		$this->tax = $input;
	}
	function getId(){
		return $this->id;
	}
	function getCountryId(){
		return $this->country_id;
	}
	function getCountryCode(){
		return $this->country_code;
	}
	function getPaymentGatewayIds(){
		return $this->payment_gateway_ids;
	}
	function getTax(){
		return $this->tax;
	}
	function init($cmd, $input)
	{
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_COUNTRY_SETTING)->result();
		}
		else if($cmd == 'country_code')
		{
			$result = $ci->db->where('country_code',$input)->get(TBL_COUNTRY_SETTING)->result();
		}else if($cmd == 'country_id'){
			$result = $ci->db->where('country_id',$input)->get(TBL_COUNTRY_SETTING)->result();
		}else if($cmd == 'tax'){
			$result = $ci->db->where('tax',$input)->get(TBL_COUNTRY_SETTING)->result();
		}else
		{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->country_id = $result[0]->country_id;
			$this->country_code = $result[0]->country_code;
			$this->payment_gateway_ids = $result[0]->payment_gateway_ids;
			$this->tax = $result[0]->tax;
			return $this;
		}
		return false;
	}
	function getCountrySettingList(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->select(TBL_COUNTRY_SETTING.'.* , '. TBL_COUNTRY.'.country_name AS country_name');
		$ci->db->join(TBL_COUNTRY , TBL_COUNTRY.'.id = '.TBL_COUNTRY_SETTING.'.country_id', 'left');
		return $ci->db->get(TBL_COUNTRY_SETTING)->result();
	}
	function save(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('country_id',$this->getCountryId());
		$ci->db->set('country_code',$this->getCountryCode());
		$ci->db->set('payment_gateway_ids',$this->getPaymentGatewayIds());
		$ci->db->set('tax',$this->getTax());
		$ci->db->insert(TBL_COUNTRY_SETTING);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('country_id',$this->getCountryId());
		$ci->db->set('country_code',$this->getCountryCode());
		$ci->db->set('payment_gateway_ids',$this->getPaymentGatewayIds());
		$ci->db->set('tax',$this->getTax());
		return $ci->db->where('id', $this->id)->update(TBL_COUNTRY_SETTING);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_COUNTRY_SETTING);
	}
	function checkExistedCountry($id, $country_id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if($id){
			$ci->db->where('id !=',$id);
		}
		$ci->db->where('country_id',$country_id);
		return $ci->db->get(TBL_COUNTRY_SETTING)->result();
	}
	function getPaymentGateway($ids){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		return $ci->db->get(TBL_COUNTRY_SETTING)->result();
	}
}